//@ts-ignore
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import * as monaco from "monaco-editor";
export class BaseEditor {
  instance: monaco.editor.IStandaloneCodeEditor;
  container: HTMLElement;
  constructor() {
    self.MonacoEnvironment = {
      getWorker(_: any, label: string) {
        return new editorWorker();
      },
    };
  }
  init() {}

  createEditor(container: HTMLElement) {
    this.container = container;
    this.instance = monaco.editor.create(container, {
      value: "",
      language: "python",
      theme: "vs-light",
      overviewRulerBorder: false,
      minimap: {
        enabled: false,
      },
    });
    this.completion();
  }

  completion() {
    monaco.languages.registerCompletionItemProvider("python", {
      provideCompletionItems: (model, position) => {
        const word = model.getWordUntilPosition(position);
        const range = {
          startLineNumber: position.lineNumber,
          endLineNumber: position.lineNumber,
          startColumn: word.startColumn,
          endColumn: word.endColumn,
        };
        return {
          suggestions: [
            {
              label: "process",
              insertText: "process()",
              kind: monaco.languages.CompletionItemKind.Function,
              detail: "主要执行函数",
              range,
            },
          ],
        };
      },
      triggerCharacters: ["$", "app."],
    });
  }

  clear() {
    this.instance.setValue("");
  }

  format() {
    this.instance.getAction("editor.action.formatDocument")?.run();
  }

  get value(): string {
    return this.instance.getValue();
  }

  set value(val: string) {
    this.instance.setValue(val);
  }
}
